package cn.beyond.vehicleManagement.controller;

import cn.beyond.vehicleManagement.pojo.SysDepartment;
import cn.beyond.vehicleManagement.pojo.Vehicles;
import cn.beyond.vehicleManagement.service.DepartmentService;
import cn.beyond.vehicleManagement.service.VehicleService;
import com.alibaba.fastjson.JSON;
import com.sun.deploy.util.UpdateCheck;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(urlPatterns = {"/departmentServlet"})
public class DepartmentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义全局变量
        DepartmentService service=new DepartmentService();
        String opr= req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        SysDepartment sysDepartment=new SysDepartment();
        Map<String,Object> map = new HashMap<>();
        PrintWriter out= resp.getWriter();
        List<SysDepartment> sysDepartmentList=new ArrayList<>();
        System.out.println(opr);
        switch (opr){
            //新增部门
         case "adddepartment":
             boolean addflag= addDepartment(req,service);
               out.print(addflag);
               break;
            //删除部门
            case "deletedepartment":
                int delId=Integer.parseInt(req.getParameter("Id"));
                boolean delflag=false;
                int delcount=service.deleteDepartment(delId,"id");
                System.out.println(delcount);
                if(delcount>0){
                    delflag=true;
                }
                out.print(delflag);
                break;
                //更新部门信息
          case "updatedepartment":
              boolean updateflag= updateDepartment(req,service);
              out.print(updateflag);
              break;
            case "selectdepartment":
                int id= Integer.parseInt(req.getParameter("Id"));
                System.out.println(id);
                map.put("id",id);
                sysDepartment=service.selectUserByOne(sysDepartment,map);
                String deptJson=JSON.toJSONString(sysDepartment);
                System.out.println(deptJson);
                out.print(deptJson);
                map.clear();
                break;
            case "listDepartment":
                //列举出采购列表
                sysDepartmentList=service.selectUserByCondition(sysDepartment,map);
                List<Object>listmap=new ArrayList<>();
                listmap.add(sysDepartmentList);
                listmap.add("重庆青鸟科技有限公司");
                String listJosn= JSON.toJSONString(listmap);
                out.write(listJosn);
                map.clear();
                break;

        }
    }

    /**
     * 新增部门
     * @param req
     * @param service
     * @return
     */
     public  boolean addDepartment(HttpServletRequest req, DepartmentService service){
        int countryId= Integer.parseInt(req.getParameter("countryId"));
        String name=req.getParameter("name");
        String manager=req.getParameter("manager");
         String remark=req.getParameter("remark");
         SysDepartment sysDepartment=new SysDepartment(null,name,countryId,manager,remark);
         int count=service.addDepartment(sysDepartment);
         boolean flag=false;
         if(count>0){
             flag=true;
         }
        return  false;
     }

    /**
     * 修改部门信息
     * @param req
     * @param service
     * @return
     */
     public boolean updateDepartment(HttpServletRequest req,DepartmentService service){

         int id= Integer.parseInt(req.getParameter("id"));
         int countryId= Integer.parseInt(req.getParameter("countryId"));
         String name=req.getParameter("name");
         String manager=req.getParameter("manager");
         String remark=req.getParameter("remark");
         SysDepartment sysDepartment=new SysDepartment(id,name,countryId,manager,remark);
         int count=service.update(sysDepartment);
         boolean flag=false;
         if(count>0){
             flag=true;
         }
         return  flag;
     }



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }
}
